/**
 * 
 */
package fr.cie.blog.controller;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import fr.cie.blog.beans.Country;
import fr.cie.blog.domainservice.IServiceCountry;
import fr.cie.blog.services.parsingservice.CountryParser;

/**
 * @author Jee3
 * 
 */
public class CountryAction extends GenericAction {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(CountryAction.class);

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 8059287422301905156L;
	/**
	 * country.
	 */
	private Country country;
	/**
	 * countryList.
	 */
	private List<Country> countryList;
	/**
	 * serviceCountry.
	 */
	private IServiceCountry serviceCountry;

	/**
	 * Constructor.
	 * 
	 * @param pServiceCountry
	 *            serviceCountry
	 */
	public CountryAction(final IServiceCountry pServiceCountry) {
		super();
		serviceCountry = pServiceCountry;
	}

	/**
	 * @return the country
	 */
	public final Country getCountry() {
		return country;
	}

	/**
	 * @return the countryList
	 */
	public final List<Country> getCountryList() {
		return countryList;
	}

	/**
	 * @return the serviceCountry
	 */
	public final IServiceCountry getServiceCountry() {
		return serviceCountry;
	}

	/**
	 * @param pcountry
	 *            the country to set
	 */
	public final void setCountry(final Country pcountry) {
		this.country = pcountry;
	}

	/**
	 * @param pcountryList
	 *            the countryList to set
	 */
	public final void setCountryList(final List<Country> pcountryList) {
		this.countryList = pcountryList;
	}

	/**
	 * @param pServiceCountry
	 *            the serviceCountry to set
	 */
	public final void setServiceCountry(final IServiceCountry pServiceCountry) {
		this.serviceCountry = pServiceCountry;
	}

	/**
	 * @return SUCCESS
	 */
	public final String showCountryList() {
		countryList = serviceCountry.findAll();
		return SUCCESS;
	}

	/**
	 * Update the list of Country in XML file.
	 * 
	 * @return SUCCESS
	 */
	public final String updateCountryList() {
		final CountryParser parser = new CountryParser();

		final URL url = generateURL("/countryList.xml");
		countryList = parser.updateCountryList(url);

		final List<Country> countryListBase = serviceCountry.findAll();

		final List<Country> countryListToAdd = new ArrayList<Country>(
				countryList);
		countryListToAdd.removeAll(countryListBase);

		serviceCountry.save(countryListToAdd);

		countryList = serviceCountry.findAll();
		LOG.debug("countryList => " + countryList.size());
		return SUCCESS;
	}

	/**
	 * Generate dynamic URL.
	 * 
	 * @param endPath
	 *            the end of the complete path
	 * @return the dynamic url
	 */
	private URL generateURL(final String endPath) {
		LOG.trace("Entering CountryAction.generateURL(" + endPath + ")");
		// get context to create dynamic root url
		final String path = getConfig().getRootPath() + endPath;
		URL url = null;

		try {
			url = new URL(path);
		} catch (MalformedURLException e) {
			LOG.error("error in CountryAction.generateURL()", e);
		}
		return url;
	}
}
